﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace NetJsWire.Widgets.Ui.Form
{
    public class ComboBoxColumnEditor : CollectionEditor
    {
        //private ComboBoxColumnEditorDialog dialog;
        private ComboBox combo;

        public ComboBoxColumnEditor( Type type )
            : base( type )
        {
        }

        protected override object SetItems( object editValue, object[] value )
        {
            if ( editValue != null )
            {
                var length = GetItems( editValue ).Length;
                var num2 = value.Length;
                if ( !( editValue is ComboBoxColumnCollection ) )
                {
                    return editValue;
                }
                var list = ( ComboBoxColumnCollection ) editValue;
                list.Clear();
                for ( var i = 0; i < value.Length; i++ )
                {
                    list.Add( value[ i ] as ComboBoxColumn );
                }
            }
            return editValue;
        }

        protected override Type CreateCollectionItemType()
        {
            return typeof (ComboBoxColumn);
        }

        protected override Type[] CreateNewItemTypes()
        {
            return new[] {typeof (ComboBoxColumn)};
        }

        public override object EditValue( ITypeDescriptorContext context, IServiceProvider provider, object value )
        {
            combo = ( ComboBox ) context.Instance;
            return base.EditValue( context, provider, value );
        }

        protected override object CreateInstance( Type itemType )
        {
            if ( itemType == typeof (ComboBoxColumn) )
            {
                return new ComboBoxColumn( combo );
            }
            return base.CreateInstance( itemType );
        }

        //public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        //{
        //    if (provider != null)
        //    {
        //        IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
        //        if ((service == null) || (context.Instance == null))
        //        {
        //            return value;
        //        }
        //        IDesignerHost host = (IDesignerHost)provider.GetService(typeof(IDesignerHost));
        //        if (host == null)
        //        {
        //            return value;
        //        }
        //        if (this.dialog == null)
        //        {
        //            this.dialog = new ComboBoxColumnEditorDialog();
        //        }
        //        this.dialog.SetComboBox((ComboBox)context.Instance);
        //        using (DesignerTransaction transaction = host.CreateTransaction("ComboBoxColumnCollectionTransaction"))
        //        {
        //            if (service.ShowDialog(this.dialog) == DialogResult.OK)
        //            {
        //                transaction.Commit();
        //                value = ((ComboBox)context.Instance).Columns;
        //                return value;
        //            }
        //            transaction.Cancel();
        //        }
        //    }
        //    return value;
        //}

        //public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        //{
        //    return UITypeEditorEditStyle.Modal;
        //}

        //internal new object GetService(Type serviceType)
        //{
        //    if (this.Context != null)
        //    {
        //        return this.Context.GetService(serviceType);
        //    }
        //    return null;
        //}

        //internal new bool CanRemoveInstance(object value)
        //{
        //    return base.CanRemoveInstance(value);
        //}

        //internal new object CreateInstance(Type itemType)
        //{
        //    return base.CreateInstance(itemType);
        //}

        //internal new bool CanSelectMultipleInstances()
        //{
        //    return base.CanSelectMultipleInstances();
        //}

        //internal new Type CollectionType
        //{
        //    get
        //    {
        //        return base.CollectionType;
        //    }
        //}

        //internal new Type CollectionItemType
        //{
        //    get
        //    {
        //        return base.CollectionItemType;
        //    }
        //}

        //internal new ITypeDescriptorContext Context
        //{
        //    get
        //    {
        //        return base.Context;
        //    }
        //}

        //internal new object[] GetItems(object editValue)
        //{
        //    return base.GetItems(editValue);
        //}

        //internal new object SetItems(object editValue, object[] value)
        //{
        //    return base.SetItems(editValue, value);
        //}
        //private Type[] newItemTypes;
        //internal new Type[] NewItemTypes
        //{
        //    get
        //    {
        //        if (newItemTypes == null)
        //        {
        //            newItemTypes = base.CreateNewItemTypes();
        //        }
        //        return newItemTypes;
        //    }
        //}
    }
}